//
//  WBHomeViewController.swift
//  仿微博
//
//  Created by 杨大伟 on 2016/12/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit


   /// 原创微博客重用 cell id
   private let originalCellId = "originalCellId"
/// 被转发微博客重用 cell id
   private let retweetedCellId = "retweetedCellId"
class WBHomeViewController: WBBaseViewController {
    
    //列表视图模型
     fileprivate lazy var listViewModel = WBStatusListViewModel()
    // 加载数据
    override func loadData() {
        print("准备刷新 最后一条 \(self.listViewModel.statusList.last?.status.text)")
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上了刷新标记
            self.isPullup = false
            //刷新 表格
            if shouldRefresh{
            self.tableView?.reloadData()
            }
            
        }
        
    }
        
        
    
    
        //显示好友
    @objc fileprivate func showFriends() {
        print(#function)
        
        let vc = WBDemoViewController()
            //push 的动作是 nav 做的
        navigationController?.pushViewController(vc, animated: true)
        
    }
   
}
//  MARK: - 表格数据源方法
extension WBHomeViewController {
    
   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    //0. 取出视图模型 ，根据视图模型判断可重用  cell
    let vm = listViewModel.statusList[indexPath.row]
    let cellId = (vm.status.retweeted_status != nil) ? retweetedCellId : originalCellId
    
    
    
        //1.取 cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        
        //2.设置 cell
    
       cell.viewModel = vm
    
    
     //  3. 返回 cell
        return cell
    }
    
    
}
//  MARK: - 设置界面


extension WBHomeViewController {
    
    //重写父类方法
    override func setupTableView() {
        
      super.setupTableView()
        
        //设置导航栏按钮
        //无高亮
       // navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        //Swift 调用  OC 返回 instancetype的方法，判断不出是否可选
       // let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
      //  btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
      //  navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        
      
       navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
//        注册原型cell

        
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        //设置行高
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        //取消分割线
        tableView?.separatorStyle = .none
        
        setupNavTitle()

    }


    /// 设置导航栏标题
    private func setupNavTitle() {
        
        let title = WBNetworkManager.shared().userAccount.screen_name
        
        let button = WBTitleButton(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton) {
        
        // 设置选中状态
        btn.isSelected = !btn.isSelected
    }
}
